package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.entity.LagouToken;
import com.lagou.edu.service.CodeService;
import com.lagou.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Optional;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private CodeService codeService;
    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Override
    public Boolean register(String email, String password, String code) {
        Integer validate = codeService.validate(email, code);
        if (validate == 1) {
            throw new RuntimeException("验证失败");
        }
        if(validate == 2){
            throw new RuntimeException("验证码失效");
        }

        LagouToken lagouToken = getLagouToken(email);
        // 已经存在
        if (lagouToken != null) {
            throw new RuntimeException("账号已经存在");
        }
        // 将token存入数据库
        String text = email + password;
        String token = DigestUtils.md5DigestAsHex(text.getBytes());
        lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        lagouToken.setToken(token);
        lagouTokenDao.save(lagouToken);
        return true;
    }

    @Override
    public Boolean isRegistered(String email) {
        LagouToken lagouToken = getLagouToken(email);
        return lagouToken != null;
    }

    @Override
    public String login(String email, String password) {
        String text = email + password;
        String token = DigestUtils.md5DigestAsHex(text.getBytes());
        LagouToken lagouToken = getLagouToken(email);
        if (lagouToken != null) {
            String actual_token = lagouToken.getToken();
            if (token.equals(actual_token)) {
                return email;
            }
        }
        return null;
    }

    @Override
    public String info(String token) {
        LagouToken lagouToken = getLagouTokenFromToken(token);
        return lagouToken.getEmail();
    }

    @Override
    public String token(String email) {
        return getLagouToken(email).getToken();
    }

    private LagouToken getLagouToken(String email) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        Optional<LagouToken> one = lagouTokenDao.findOne(Example.of(lagouToken));
        if (one.isEmpty()) {
            return null;
        }
        lagouToken = one.get();
        return lagouToken;
    }
    private LagouToken getLagouTokenFromToken(String token) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);
        Optional<LagouToken> one = lagouTokenDao.findOne(Example.of(lagouToken));
        if (one.isEmpty()) {
            return null;
        }
        lagouToken = one.get();
        return lagouToken;
    }
}
